#include "PowerFlowCalculation.h"
static Node m_Node[1000];
Form_IPM Form_IPM_data16(gsl_matrix *PQ_Node,gsl_matrix *PV_Node,gsl_matrix *ESS_Node,gsl_matrix *Swing_Node,int Node_Num)
{
    Form_IPM fi;

    int PQ_Num=PQ_Node->size1;
    int PV_Num=PV_Node->size1;
    int ESS_Num=ESS_Node->size1;
    int p_Num=Node_Num-Swing_Node->size1;
    double Total_PLoad=0,Total_QLoad=0,V_based=1;


    for(int i=0;i<PQ_Num;i++){
        m_Node[i].Type=0;
        m_Node[i].NodeID=gsl_matrix_get(PQ_Node,i,0);
        m_Node[i].Vol=1*V_based;
        m_Node[i].VolUpLmt=1.05*V_based;
        m_Node[i].VolDnLmt=0.95*V_based;
        m_Node[i].Angle=0;
        m_Node[i].Pl=gsl_matrix_get(PQ_Node,i,1);
        m_Node[i].Ql=gsl_matrix_get(PQ_Node,i,2);
        Total_PLoad+=m_Node[i].Pl;
        Total_QLoad+=m_Node[i].Ql;
        m_Node[i].Pg=0;
        m_Node[i].Qg=0;
        m_Node[i].P=0;
        m_Node[i].Q=0;
        m_Node[i].QUpLmt=0;
        m_Node[i].QDnLmt=0;
    }

    int k=1;
    for(int i=0;i<PV_Num;i++){
        m_Node[i+PQ_Num].Type=1;
        m_Node[i+PQ_Num].NodeID=gsl_matrix_get(PV_Node,i,0);
        m_Node[i+PQ_Num].Vol=gsl_matrix_get(PV_Node,i,2);
        m_Node[i+PQ_Num].VolUpLmt=1.1 *V_based;
        m_Node[i+PQ_Num].VolDnLmt=0.9*V_based;
        m_Node[i+PQ_Num].Angle=0;
        
        //printf("PV_Num=%d\n",PV_Num);
        
        m_Node[i+PQ_Num].Pl=gsl_matrix_get(PV_Node,i,5);
        
        m_Node[i+PQ_Num].Ql=gsl_matrix_get(PV_Node,i,6);
        m_Node[i+PQ_Num].Pg=gsl_matrix_get(PV_Node,i,1);
        
        Total_PLoad+=m_Node[i+PQ_Num].Pl;
        Total_QLoad+=m_Node[i+PQ_Num].Ql;
        m_Node[i+PQ_Num].Qg=0;
        m_Node[i+PQ_Num].P=0;
        m_Node[i+PQ_Num].Q=0;
        m_Node[i+PQ_Num].QUpLmt=gsl_matrix_get(PV_Node,i,8);
        m_Node[i+PQ_Num].QDnLmt=gsl_matrix_get(PV_Node,i,9);
    }
    
    int All_Num=PQ_Num+PV_Num;
    for(int i=0;i<ESS_Num;i++){
        m_Node[i+All_Num].Type=3;
        m_Node[i+All_Num].NodeID=gsl_matrix_get(ESS_Node,i,0);
        m_Node[i+All_Num].Vol=gsl_matrix_get(ESS_Node,i,2);
        m_Node[i+All_Num].VolUpLmt=1.1*V_based;
        m_Node[i+All_Num].VolDnLmt=0.9*V_based;
        m_Node[i+All_Num].Angle=0;
        m_Node[i+All_Num].Pl=gsl_matrix_get(ESS_Node,i,5);
        m_Node[i+All_Num].Ql=gsl_matrix_get(ESS_Node,i,6);
        m_Node[i+All_Num].Pg=gsl_matrix_get(ESS_Node,i,1);
        Total_PLoad+=m_Node[i+All_Num].Pl;
        Total_QLoad+=m_Node[i+All_Num].Ql;
        m_Node[i+All_Num].Qg=0;
        m_Node[i+All_Num].P=0;
        m_Node[i+All_Num].Q=0;
        m_Node[i+All_Num].QUpLmt=gsl_matrix_get(ESS_Node,i,8);
        m_Node[i+All_Num].QDnLmt=gsl_matrix_get(ESS_Node,i,9);
    }

    m_Node[Node_Num-1].Type=2;

   
    m_Node[Node_Num-1].NodeID=gsl_matrix_get(Swing_Node,0,0);
     
    m_Node[Node_Num-1].Vol=gsl_matrix_get(Swing_Node,0,1);

    m_Node[Node_Num-1].VolUpLmt=1.1*V_based;
    m_Node[Node_Num-1].VolDnLmt=0.9*V_based;
    m_Node[Node_Num-1].Angle=0;
    

    m_Node[Node_Num-1].Pl=gsl_matrix_get(Swing_Node,0,5);
    m_Node[Node_Num-1].Ql=gsl_matrix_get(Swing_Node,0,6);
    m_Node[Node_Num-1].Pg=0;
    Total_PLoad+=m_Node[Node_Num-1].Pl;
    Total_QLoad+=m_Node[Node_Num-1].Ql;
    m_Node[Node_Num-1].Qg=0;
    m_Node[Node_Num-1].P=0;
    m_Node[Node_Num-1].Q=0;

    
    fi.GenCount=0;
    for(int i=0;i<Node_Num;i++){
        if(m_Node[i].Type==1){
            fi.GenCount++;
        }
        if(m_Node[i].Type==3){
            fi.GenCount++;
        }

    }
    
    fi.nVaribles=fi.GenCount+2*Node_Num-1;
    fi.nEquals=2*Node_Num-1;
    fi.nNequals=fi.GenCount+Node_Num;

    fi.Node_Num=Node_Num;
    fi.Total_PLoad=Total_PLoad;
    fi.Total_QLoad=Total_QLoad;

    fi.m_Node=m_Node;
    return fi;
}

